package com.fastdfs.service.impl;

import com.fastdfs.dao.ImageitemsMapper;
import com.fastdfs.pojo.Imageitems;
import com.fastdfs.service.ImageitemsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
@Service
public class ImageitemsServiceImpl implements ImageitemsService {
    @Autowired
    private ImageitemsMapper imageitemsMapper;


    /**
     * Imageitems条件+分页查询
     * @param Imageitems 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Imageitems> findPage(Imageitems Imageitems, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(Imageitems);
        //执行搜索
        return new PageInfo<Imageitems>(imageitemsMapper.selectByExample(example));
    }

    /**
     * Imageitems分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Imageitems> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Imageitems>(imageitemsMapper.selectAll());
    }

    /**
     * Imageitems条件查询
     * @param Imageitems
     * @return
     */
    @Override
    public List<Imageitems> findList(Imageitems Imageitems){
        //构建查询条件
        Example example = createExample(Imageitems);
        //根据构建的条件查询数据
        return imageitemsMapper.selectByExample(example);
    }


    /**
     * Imageitems构建查询对象
     * @param Imageitems
     * @return
     */
    public Example createExample(Imageitems Imageitems){
        Example example=new Example(Imageitems.class);
        Example.Criteria criteria = example.createCriteria();
        if(Imageitems!=null){
            // 编号
            if(!StringUtils.isEmpty(Imageitems.getId())){
                criteria.andEqualTo("id",Imageitems.getId());
            }

            // 相册名称
            if(!StringUtils.isEmpty(Imageitems.getName())){
                criteria.andLike("name","%"+Imageitems.getName()+"%");
            }

            if(!StringUtils.isEmpty(Imageitems.getImageDescribe())){
                criteria.andLike("image_describe","%"+Imageitems.getImageDescribe()+"%");
            }


            if(!StringUtils.isEmpty(Imageitems.getImageGroup())){;
                criteria.andEqualTo("image_group",Imageitems.getImageGroup());
            }

            if(!StringUtils.isEmpty(Imageitems.getImage())){
                criteria.andEqualTo("image","%"+Imageitems.getImage()+"%");
            }

            if(!StringUtils.isEmpty(Imageitems.getImageType())){
                criteria.andEqualTo("image_type",Imageitems.getImageType());
            }
            if(!StringUtils.isEmpty(Imageitems.getImageSize())){
                criteria.andEqualTo("image_size",Imageitems.getImageSize());
            }
            if(!StringUtils.isEmpty(Imageitems.getImagePx())){
                criteria.andEqualTo("image_px",Imageitems.getImagePx());
            }
            if(!StringUtils.isEmpty(Imageitems.getImageData())){
                criteria.andEqualTo("image_data",Imageitems.getImageData());
            }

        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        imageitemsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteCondition(Imageitems record) {
        Example example = createExampleDelete(record);
        return imageitemsMapper.deleteByExample(example);
    }

    //条件进行删除
    public Example  createExampleDelete(Imageitems record){
        Example example=new Example(Imageitems.class);
        Example.Criteria criteria = example.createCriteria();
        if(record!=null){
            if(!StringUtils.isEmpty(record.getImage())){
                criteria.andEqualTo("image",record.getImage());
            }
        }
        return example;
    }


    /**
     * 修改Imageitems
     * @param Imageitems
     */
    @Override
    public void update(Imageitems Imageitems){
        imageitemsMapper.updateByPrimaryKey(Imageitems);
    }

    /**
     * 增加Imageitems
     * @param Imageitems
     */
    @Override
    public void add(Imageitems Imageitems) throws Exception {
        Imageitems imageitems = new Imageitems();
        imageitems.setName(Imageitems.getName());
        int i = imageitemsMapper.selectCount(imageitems);
        if(i>=1){
            throw  new Exception("有此图片了");
        }else {
            imageitemsMapper.insertSelective(Imageitems);
        }


    }

    /**
     * 根据ID查询Imageitems
     * @param id
     * @return
     */
    @Override
    public Imageitems findById(String id){
        return  imageitemsMapper.selectByPrimaryKey(id);
    }



    /**
     * 查询Imageitems全部数据
     * @return
     */
    @Override
    public List<Imageitems> findAll() {
        return imageitemsMapper.selectAll();
    }

    @Override
    public int count(Imageitems Imageitems) {
        return imageitemsMapper.selectCount(Imageitems);
    }

}
